home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 2.0.1 / Experimental enhancements / MacApp Sizers / SizerTest.p < prev    next >
Text File  |  1990-11-08  |  13KB  |  531 lines

  1. PROGRAM Test;
  2.  
  3. USES
  4.     UMacApp,
  5.  
  6.     {  Building Blocks }
  7.     UPrinting, UGridView, UTEView, UDialog,
  8.     UArray, USizerView,
  9.  
  10.     { ToolBox }
  11.     Types, QuickDraw, Packages,
  12.     Fonts, Resources, Strings, ToolUtils,        { ToolIntf }
  13.     OSUtils, Files, Errors, Memory;                { OSIntf }
  14.  
  15. CONST
  16.         kSignature            = 'Test';            { Application signature}
  17.         kFileType            = 'test';            { file type of parsed data file. }
  18.  
  19.         { Resource ids }
  20.         kTestWindowID        = 1001;                { main window }
  21.         kStringList1        = 2000;                { STR# resource for left pane }
  22.         kStringList2        = 2001;                { STR# resource for second pane }
  23.         kStringList3        = 2002;                { STR# resource for third pane }
  24.         kStringList4        = 2003;                { STR# resource for right pane }
  25.  
  26.         { Commands }
  27.         cNewSplitHWindow    = 1205;                { Open a new window with horizontal sizer & splitter }
  28.         cNewSplitVWindow    = 1206;                { Open a new window with vertical sizer & splitter }
  29.  
  30.         kMinH                = 4*kMinSizerPane + 3*kSizerThickness;    { 4 panes across }
  31.         kMinV                = 2*kMinSizerPane + kSizerThickness;    { 2 stacked panes }
  32.  
  33. TYPE
  34.  
  35.     TTestApplication = OBJECT (TApplication)
  36.  
  37.         PROCEDURE TTestApplication.ITestApplication;
  38.         { Initializes the application and globals. }
  39.  
  40.         FUNCTION  TTestApplication.DoMakeDocument(itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  41.         { Create a document object that will create the window. }
  42.         
  43.         FUNCTION  TTestApplication.DoMenuCommand(aCmdNumber: CmdNumber): TCommand; OVERRIDE;
  44.         PROCEDURE TTestApplication.DoSetupMenus; OVERRIDE;
  45.  
  46.         END;
  47.  
  48.     TTestDocument = OBJECT(TDocument)
  49.  
  50.         fListViews:            TList;            { of TTestListViews }
  51.         fTextView:            TTestView;        { shows current list selections }
  52.  
  53.         PROCEDURE TTestDocument.ITestDocument(itsCmdNumber: CmdNumber);
  54.         { Initialize the test document. }
  55.         
  56.         PROCEDURE TTestDocument.Free; OVERRIDE;
  57.         { Free the list we created }
  58.  
  59.         PROCEDURE TTestDocument.ChangeData;
  60.         { Change fTextView's data to reflect current selection }
  61.  
  62.         PROCEDURE TTestDocument.DoChoice(origView: TView; itsChoice: INTEGER); OVERRIDE;
  63.         { Handle communication from subviews }
  64.  
  65.         PROCEDURE TTestDocument.DoMakeViews(forPrinting: BOOLEAN); OVERRIDE;
  66.         { Create the window and views to display the data. }
  67.         
  68.         END;
  69.  
  70.     TTestListView    = OBJECT (TTextListView)
  71.     { This list view gets its items from a STR# resource }
  72.         
  73.         fStringListId:        INTEGER;        { Id of STR# resource }
  74.  
  75.         PROCEDURE TTestListView.ITestListView(itsStringList: INTEGER);
  76.         { Tell the view which STR# resource to get its items from. Set the number
  77.           of items to be the same as the number of strings in the resource. }
  78.  
  79.         PROCEDURE TTestListView.GetItemText(anItem: INTEGER; VAR aString: Str255);  OVERRIDE;
  80.  
  81.         PROCEDURE TTestListView.SelectItem(anItem: INTEGER; extendSelection, highlight,
  82.                                            select: BOOLEAN);  OVERRIDE;
  83.         
  84.         END;
  85.  
  86.     TTestView    = OBJECT (TView)
  87.  
  88.         fDataHandle:        Handle;            { text for display }
  89.         fTextStyle:            TextStyle;        { style for displaying text }
  90.  
  91.         PROCEDURE TTestView.IRes(itsDocument: TDocument; itsSuperView: TView;
  92.                                  VAR itsParams: Ptr); OVERRIDE;
  93.  
  94.         PROCEDURE TTestView.Draw(area: Rect); OVERRIDE;
  95.         { Draw the current selections of the TTextListViews }
  96.  
  97.         PROCEDURE TTestView.Free; OVERRIDE;
  98.         { Free any leftover data }
  99.  
  100.         PROCEDURE TTestView.SetData(newData: Handle);
  101.         { Set the data handle to the given handle. Dispose the old one }
  102.  
  103.         END;
  104.         
  105.     TStupidView    = OBJECT (TView)
  106.     { A stupid view that does nothing but draw something }
  107.  
  108.         fNumber:        INTEGER;
  109.  
  110.         PROCEDURE TStupidView.IStupidView(itsNumber: INTEGER);
  111.  
  112.         FUNCTION  TStupidView.Clone: TObject; OVERRIDE;
  113.  
  114.         PROCEDURE TStupidView.Draw(area: Rect); OVERRIDE;
  115.  
  116.         FUNCTION  TStupidView.GetNumber: INTEGER;
  117.  
  118.         END;
  119.  
  120.  
  121. VAR
  122.     gTestApplication:        TTestApplication;
  123.  
  124.  
  125. {------------------------ TTestApplication ------------------------------------------}
  126.  
  127. {$S AInit}
  128.  
  129. PROCEDURE TTestApplication.ITestApplication;
  130.  
  131.     BEGIN
  132.     InitUSizerView;                        { Init the gnarly split pane meister. }
  133.  
  134.     IApplication(kFileType);
  135.  
  136.     { Suppress dead-stripping of classes created from view templates }
  137.     IF gDeadStripSuppression THEN BEGIN
  138.         IF Member(TObject(NIL), TTestView) THEN;
  139.         IF Member(TObject(NIL), TTestListView) THEN;
  140.         IF Member(TObject(NIL), TStupidView) THEN;
  141.         END;
  142.     END;
  143.  
  144. FUNCTION TTestApplication.DoMakeDocument(itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  145. { Called by OpenNew and OpenOld: cNew and cOpen }
  146.  
  147.     VAR
  148.         aTestDocument:        TTestDocument;
  149.         fi:                    FailInfo;
  150.  
  151.     PROCEDURE InitFailed(error: OSErr; message: LONGINT);
  152.         BEGIN
  153.         FreeIfObject(aTestDocument);
  154.         END;
  155.  
  156.     BEGIN
  157.     { Allocate and initialize a test document. }
  158.     NEW(aTestDocument);
  159.     FailNIL(aTestDocument);
  160.  
  161.     CatchFailures(fi, InitFailed);
  162.     aTestDocument.ITestDocument(itsCmdNumber);
  163.     Success(fi);
  164.  
  165.     DoMakeDocument := aTestDocument;
  166.     END;
  167.  
  168. {$S ASelCommand}
  169.  
  170. FUNCTION TTestApplication.DoMenuCommand(aCmdNumber: CmdNumber): TCommand; OVERRIDE;
  171.  
  172.     PROCEDURE MakeSplitterWindow(windId: INTEGER; sizerId: IDType);
  173.  
  174.         VAR
  175.             aWindow:        TWindow;
  176.             aSizerView:        TSizerView;
  177.             aStupidView:    TStupidView;
  178.  
  179.         BEGIN
  180.         aWindow := NewTemplateWindow(windId, NIL);
  181.         FailNIL(aWindow);
  182.  
  183.         aSizerView := TSizerView(aWindow.FindSubview(sizerId));
  184.         IF qDebug THEN FailNIL(aSizerView);
  185.         aSizerView.FixupPanes(FALSE);
  186.  
  187.         aStupidView := TStupidView(aWindow.FindSubview('stpd'));
  188.         IF qDebug THEN FailNIL(aStupidView);
  189.         aStupidView.IStupidView(aCmdNumber);
  190.         
  191.         aWindow.Open;
  192.         END;
  193.  
  194.     BEGIN
  195.     CASE aCmdNumber OF
  196.         cNewSplitHWindow: MakeSplitterWindow(aCmdNumber, 'hsiz');
  197.         cNewSplitVWindow: MakeSplitterWindow(aCmdNumber, 'vsiz');
  198.  
  199.         OTHERWISE
  200.             DoMenuCommand := INHERITED DoMenuCommand(aCmdNumber);
  201.         END;
  202.     END;
  203.  
  204. {$S ARes}
  205.  
  206. PROCEDURE TTestApplication.DoSetupMenus; OVERRIDE;
  207.  
  208.     BEGIN
  209.     INHERITED DoSetupMenus;
  210.  
  211.     Enable(cNewSplitHWindow, TRUE);
  212.     Enable(cNewSplitVWindow, TRUE);
  213.     END;
  214.  
  215. {-------------------------- TTestDocument -------------------------------------------}
  216.  
  217. {$S AOpen}
  218.  
  219. PROCEDURE TTestDocument.ITestDocument(itsCmdNumber: CmdNumber);
  220.  
  221.     BEGIN
  222.     fTextView := NIL;
  223.     fListViews := NIL;
  224.  
  225.     IDocument(kFileType, kSignature, NOT kUsesDataFork, NOT kUsesRsrcFork, 
  226.               NOT kDataOpen, NOT kRsrcOpen);
  227.                     
  228.     fListViews := NewList;
  229.     fListViews.SetEltType('TTestListView');
  230.     END;
  231.  
  232. {$S AClose}
  233.  
  234. PROCEDURE TTestDocument.Free; OVERRIDE;
  235.  
  236.     BEGIN
  237.     fListViews.DeleteAll;
  238.     fListViews.Free;
  239.     INHERITED Free;
  240.     END;
  241.  
  242. {$S ADoCommand}
  243.  
  244. PROCEDURE TTestDocument.ChangeData;
  245.  
  246.     VAR
  247.         newData:        Handle;
  248.         s:                INTEGER;
  249.         offset:            LONGINT;
  250.         newSize:        LONGINT;
  251.         aListView:        TTestListView;
  252.         aString:        Str255;
  253.  
  254.     BEGIN
  255.     newData := NewHandle(0);
  256.     FailNIL(newData);
  257.  
  258.     FOR s := 1 TO fListViews.GetSize DO BEGIN
  259.         aListView := TTestListView(fListViews.At(s));
  260.         WITH aListView DO                            { get the view's current selection }
  261.             GetItemText(FirstSelectedItem, aString);
  262.         { Append aString’s text part to handle data }
  263.         offset := GetHandleSize(newData);
  264.         newSize := offset + LENGTH(aString) + 1;    { allow for separator char }
  265.         SetHandleSize(newData, newSize);            { make room for new stuff }
  266.         newSize := LENGTH(aString) + 1;                { number of chars to copy }
  267.         aString[0] := CHR($0D);                        { separate with CR }
  268.         BlockMove(@aString, Ptr(StripLong(newData^)+offset), newSize);
  269.         END;
  270.  
  271.     fTextView.SetData(newData);
  272.     END;
  273.  
  274. {$S ADoCommand}
  275.  
  276. PROCEDURE TTestDocument.DoChoice(origView: TView; itsChoice: INTEGER); OVERRIDE;
  277.  
  278.     VAR
  279.         id:                IDType;
  280.  
  281.     BEGIN
  282.     IF itsChoice = mListItemHit THEN        { user selected a list item }
  283.         ChangeData;                            { change data to reflect new selection }
  284.     END;
  285.  
  286. {$S AOpen}
  287.  
  288. PROCEDURE TTestDocument.DoMakeViews(forPrinting: BOOLEAN); OVERRIDE;
  289. { Create the views for this hog.   All of them.  In the city and in the streets. }
  290.  
  291.     VAR
  292.         aWindow:                TWindow;
  293.         minSize:                Point;
  294.         aSizerView:                TSizerView;
  295.  
  296.     PROCEDURE DoPostRes(viewId: IDType);
  297.  
  298.         VAR
  299.             aSizerView:            TSizerView;
  300.  
  301.         BEGIN
  302.         aSizerView := TSizerView(aWindow.FindSubview(viewId));
  303.         IF qDebug THEN FailNIL(aSizerView);
  304.         aSizerView.FixupPanes(TRUE);        { equally spaced panes }
  305.         END;
  306.  
  307.     PROCEDURE InitListView(viewId: IDType; itsStringList: INTEGER);
  308.  
  309.         VAR
  310.             aListView:            TTestListView;
  311.  
  312.         BEGIN
  313.         aListView := TTestListView(aWindow.FindSubview(viewId));
  314.         IF qDebug THEN FailNIL(aListView);
  315.         aListView.ITestListView(itsStringList);
  316.         fListViews.InsertLast(aListView);
  317.         END;
  318.  
  319.     BEGIN
  320.     aWindow := NewTemplateWindow(kTestWindowID, SELF);
  321.     FailNIL(aWindow);
  322.  
  323.     { Make sure that the minimum size for the window is reasonable for the
  324.       TSizerViews we are using. }
  325.     SetPt(minSize, kMinH, kMinV);
  326.     aWindow.SetResizeLimits(minSize, gStdWSizeRect.botRight);
  327.  
  328.     { Set the thickness of the splitter bars (do this before FixupPanes) }
  329.     aSizerView := TSizerView(aWindow.FindSubview('main'));
  330.     aSizerView.SetSizerThickness(10);
  331.  
  332.     { Finish installing the subviews in the TSizerViews }
  333.     DoPostRes('main');
  334.     DoPostRes('uppr');
  335.     fTextView := TTestView(aWindow.FindSubview('lowr'));
  336.  
  337.     { Finish initializing the list views }
  338.     InitListView('aaaa', kStringList1);
  339.     InitListView('bbbb', kStringList2);
  340.     InitListView('cccc', kStringList3);
  341.     InitListView('dddd', kStringList4);
  342.  
  343.     { Set the minimum width for the list panes }
  344.     aSizerView := TSizerView(aWindow.FindSubview('uppr'));
  345.     aSizerView.SetMinPaneLength(30);
  346.     END;
  347.  
  348. {-------------------------- TTestListView -------------------------------------------}
  349.  
  350. {$S AOpen}
  351.  
  352. PROCEDURE TTestListView.ITestListView(itsStringList: INTEGER);
  353.  
  354.     VAR
  355.         items:        INTEGER;
  356.  
  357.     FUNCTION CountStrings(strID: INTEGER): INTEGER;
  358.     { Return the number of strings contained in the specified STR# resource }
  359.     
  360.         TYPE
  361.             strResource = RECORD
  362.                     count:        INTEGER;
  363.                     firstStr:    Str255;        { actually, an array of variable-length strings }
  364.                     END;
  365.             strPointer = ^strResource;
  366.             strHandle  = ^strPointer;
  367.     
  368.         VAR
  369.             strRes:        strHandle;
  370.     
  371.         BEGIN
  372.         strRes := strHandle(GetResource('STR#', strID));
  373.         IF strRes = NIL
  374.             THEN CountStrings := 0
  375.             ELSE CountStrings := strRes^^.count;
  376.         END;
  377.     
  378.     BEGIN
  379.     fStringListId := itsStringList;
  380.     items := CountStrings(itsStringList);
  381.     InsItemLast(items);
  382.     END;
  383.  
  384. {$S ARes}
  385.  
  386. PROCEDURE TTestListView.GetItemText(anItem: INTEGER; VAR aString: Str255);  OVERRIDE;
  387.  
  388.     BEGIN
  389.     IF anItem = 0
  390.         THEN aString := ''
  391.         ELSE GetIndString(aString, fStringListId, anItem);
  392.     END;
  393.  
  394. {$S ASelCommand}
  395.  
  396. PROCEDURE TTestListView.SelectItem(anItem: INTEGER; extendSelection, highlight, select: BOOLEAN);  OVERRIDE;
  397.  
  398.     BEGIN
  399.     INHERITED SelectItem(anItem, extendSelection, highlight, select);
  400.  
  401.     fDocument.DoChoice(SELF, mListItemHit);        { inform the document }
  402.     END;
  403.  
  404. {---------------------------- TTestView ---------------------------------------------}
  405.  
  406. {$S AOpen}
  407.  
  408. PROCEDURE TTestView.IRes(itsDocument: TDocument; itsSuperView: TView;
  409.                          VAR itsParams: Ptr); OVERRIDE;
  410.  
  411.     VAR
  412.         aTextStyle:        TextStyle;
  413.  
  414.     BEGIN
  415.     fDataHandle := NIL;
  416.     INHERITED IRes(itsDocument, itsSuperView, itsParams);
  417.  
  418.     SetTextStyle(aTextStyle, 0, [], 12, gRGBBlack);    { System font, plain, 12 point }
  419.     fTextStyle := aTextStyle;
  420.     END;
  421.  
  422. {$S AClose}
  423.  
  424. PROCEDURE TTestView.Free; OVERRIDE;
  425.  
  426.     BEGIN
  427.     fDataHandle := DisposeIfHandle(fDataHandle);
  428.     INHERITED Free;
  429.     END;
  430.  
  431. {$S ARes}
  432.  
  433. PROCEDURE TTestView.Draw(area: Rect); OVERRIDE;
  434.  
  435.     VAR
  436.         box:        Rect;
  437.         itsLength:    LONGINT;
  438.         aTextStyle:    TextStyle;
  439.  
  440.     BEGIN
  441.     IF fDataHandle <> NIL THEN BEGIN        { there’s something to draw }
  442.         aTextStyle := fTextStyle;
  443.         SetPortTextStyle(aTextStyle);
  444.         GetQDExtent(box);
  445.         InsetRect(box, 5, 0);                { leave some margin on the sides }
  446.         itsLength := GetHandleSize(fDataHandle);
  447.         LockHandleHigh(fDataHandle);         { because MATextBox may move memory }
  448.         MATextBox(fDataHandle^, itsLength, box, teJustSystem, kAutoWrap, NIL, kNoEraseFirst, kNoSpaceForCaret);
  449.         HUnlock(fDataHandle);
  450.         END;
  451.     INHERITED Draw(area);
  452.     END;
  453.  
  454. {$S ADoCommand}
  455.  
  456. PROCEDURE TTestView.SetData(newData: Handle);
  457.  
  458.     BEGIN
  459.     fDataHandle := DisposeIfHandle(fDataHandle);
  460.     fDataHandle := newData;
  461.     ForceRedraw;
  462.     END;
  463.  
  464. {---------------------------- TStupidView ---------------------------------------------}
  465.  
  466. {$S AOpen}
  467.  
  468. PROCEDURE TStupidView.IStupidView(itsNumber: INTEGER);
  469.  
  470.     BEGIN
  471.     fNumber := itsNumber;
  472.     END;
  473.  
  474. {$S ARes}
  475.  
  476. PROCEDURE TStupidView.Draw(area: Rect); OVERRIDE;
  477.  
  478.     VAR
  479.         aString:    Str255;
  480.  
  481.     BEGIN
  482.     TextFont(0);
  483.     TextSize(12);
  484.     MoveTo(10, 20);    (* h, v *)
  485.     NumToString(fNumber, aString);
  486.     aString := concat('duh…', aString);
  487.     DrawString(aString);
  488.  
  489.     INHERITED Draw(area);
  490.     END;
  491.  
  492. FUNCTION TStupidView.GetNumber: INTEGER;
  493.  
  494.     BEGIN
  495.     GetNumber := fNumber;
  496.     END;
  497.  
  498. FUNCTION TStupidView.Clone: TObject; OVERRIDE;
  499.  
  500.     VAR
  501.         theClone: TStupidView;
  502.  
  503.     BEGIN
  504.     theClone := TStupidView(INHERITED Clone);
  505.     theClone.IStupidView(fNumber + 1);            { so we can tell them apart! }
  506.     Clone := theClone;
  507.     END;
  508.  
  509. {------------------------------------------------------------------------------------}
  510.     { T H E   M A I N    P R O G R A M }
  511. {$S Main}
  512.  
  513. BEGIN
  514.     InitToolBox;                                    { Essential toolbox and utilities
  515.                                                       initialization }
  516.     IF ValidateConfiguration(gConfiguration) THEN    { Make sure we can run }
  517.         BEGIN
  518.         InitUMacApp(20);                             { Initialize the Toolbox, making lots of calls to MoreMasters }
  519.         InitUTEView;                                { Initialize TEView unit }
  520.         InitUDialog;                                { Initialize other units }
  521.         InitUGridView;
  522.  
  523.         NEW(gTestApplication);                        { Allocate a new TTestApplication object }
  524.         FailNIL(gTestApplication);
  525.         gTestApplication.ITestApplication;            { Initialize that new object }
  526.     
  527.         gTestApplication.Run;                        { Run the application. When it's done, exit. }
  528.         END
  529.     ELSE
  530.         StdAlert(phUnsupportedConfiguration);
  531. END.